package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.prop.UserProperties;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴倩芸
 * @create 2022/6/13
 **/
@RestController
@RequestMapping("/system/oss")
public class SysOssController extends BaseController {

    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private UserProperties userProperties;

    @ApiOperation("查询oss对象列表")
    @GetMapping("/query/{ids}")
    public AjaxResult queryOssByIds(@PathVariable("ids") String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] split = ids.split(",");
            List<String> list = new ArrayList<>();
            for (String s : split) {
                try {
                    list.add(s);
                } catch (Exception e) {
                    continue;
                }
            }
            return AjaxResult.success(sysOssService.queryListByIds(list));
        }
        return AjaxResult.error();
    }

    @ApiOperation("传入ossid查询oss对象")
    @GetMapping("/query")
    public AjaxResult queryOssById(@RequestParam("id") String id) {
        try {
            SysOss sysOss = sysOssService.queryById(id);
            return AjaxResult.success(sysOss);
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = File.class, required = true),
    })
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        if (null == file) {
            return AjaxResult.error("上传文件不能为空", null);
        }
        try {
            SysOss oss = sysOssService.upload(file);
            return AjaxResult.success(oss);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("传入ossId删除文件")
    @GetMapping("/delete/{ossId}")
    public AjaxResult deleteFile(@PathVariable("ossId") String[] ossIds) {
        Boolean ans = sysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true);
        if (ans) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }


    @ApiOperation("下载模板文件")
    @GetMapping("/download")
    public void downloadFile(@RequestParam(value = "templateFile", required = true) String templateFile,
                                     HttpServletResponse response) throws IOException {
        byte[] data = sysOssService.download(templateFile);
        genCode(response, data);
    }

    /**
     * 生成文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"templateFile.xls\"");
//        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
